package TUILayer;
import ControlLayer.*;

public class DVDUI
{

    private DVDCtr dvdCtr;
    private CopyUI copyUI;
    private InputParser inputParser;
    
    public DVDUI(InputParser inputParser)
    {
        dvdCtr = new DVDCtr();
        this.inputParser = inputParser;
        copyUI = new CopyUI(dvdCtr, inputParser);
    }

    public void start()
    {
        int choise = 0;
        boolean exit = false;
        while(choise < 1 || choise > 6 || !exit)
        {
            choise = inputParser.getInt("\f *** DVDMenu ***\n\n1. Select DVD\n2. Add DVD\n3. Delete DVD\n4. Update DVD\n5. List all DVDs\n6. Return\n\nMake your choise: ");
            if(choise == 1)
            {
                copyUI.start();
            }
            else if(choise == 2)
            {
                createDVD();
            }
            else if(choise == 3)
            {
                deleteDVD();
            }
            else if(choise == 4)
            {
                updateDVD();
            }
            else if(choise == 5)
            {
                listAllDVDs();
            }
            else if(choise == 6)
            {
                exit = true;
            }

        }
    }

    private void createDVD()
    {
        int id = inputParser.getInt("\f ***CreateDVDMenu ***\n\nPlease input the id of the new DVD: ");
        if(dvdCtr.getDVD(id) == null)
        {
            String title = inputParser.getString("\f ***CreateDVDMenu ***\n\nPlease input the title of the new DVD: ");
            String artist = inputParser.getString("\f ***CreateDVDMenu ***\n\nPlease input the artist of the new DVD: ");
            long publicationDate = inputParser.getDate("\f ***CreateDVDMenu ***\n\nPlease input the publication date (dd/mm/yyyy) of the new DVD: ");
            dvdCtr.createDVD(id, title, artist, publicationDate);
            System.out.println("\f ***CreateDVDMenu ***\n\nDVD added.");
        }
        else
        {
            System.out.println("\f ***CreateDVDMenu ***\n\nError: Id already exists in the DVD Collection");
        }
        inputParser.printReturn();
    }

    private void deleteDVD()
    {
        int id = inputParser.getInt("\f ***DeleteDVDMenu ***\n\nPlease input the id of the DVD: ");
        System.out.println("\f ***DeleteDVDMenu ***\n");
        if(dvdCtr.getDVD(id) != null)
        {
            dvdCtr.deleteDVD(id);
            System.out.println("DVD deleted.");
        }
        else
        {
            System.out.println("Error: DVD not found");
        }
        inputParser.printReturn();
    }

    private void listAllDVDs()
    {
        System.out.println("\f ***ListAllDVDMenu ***\n");
        dvdCtr.printAllDVD();
        inputParser.printReturn();
    }

    private void updateDVD()
    {
        int id = inputParser.getInt("\f ***UpdateDVDMenu ***\n\nPlease input the id of the DVD: ");
        if(dvdCtr.getDVD(id) != null)
        {
            int choise = 0;
            String value = "";
            boolean exit = false;
            while(choise > 0 && choise < 5 || !exit)
            {
                choise = inputParser.getInt("\f ***UpdateDVDMenu ***\n\n1. Edit title\n2. Edit artist\n3. Edit publication date\n4. Return\n\nMake your choise: ");
                if(choise > 0 && choise < 5)
                {
                    if(choise == 4)
                    {
                        choise = 5;
                        exit = true;
                    }
                    else
                    {
                        if(choise == 1)
                        {
                            value = inputParser.getString("\f ***UpdateDVDMenu ***\n\nPlease input the new title of the DVD: ");
                        }
                        else if(choise == 2)
                        {
                            value = inputParser.getString("\f ***UpdateDVDMenu ***\n\nPlease input the new artist of the DVD: ");
                        }
                        else if(choise == 3)
                        {
                            value = String.valueOf(inputParser.getDate("\f ***UpdateDVDMenu ***\n\nPlease input the new publication date (dd/mm/yyyy) of the DVD: "));
                        }
                        dvdCtr.updateDVD(choise, id, value);
                        System.out.println("\f ***UpdateDVDMenu ***\n\nDVD updated");
                        inputParser.printReturn();
                    }
                }
            }
        }
        else
        {
            System.out.println("\f ***UpdateDVDMenu ***\n\nError: DVD not found");
            inputParser.printReturn();
        }
    }

}
